var t = require("../../../utils/qiniuUploader"), a = getApp();

Page({
    data: {
        content: "",
        images: [],
        token: ""
    },
    inputChange: function(t, a) {
        this.setData({
            content: t.detail.content
        });
    },
    clickAdd: function(t) {
        var e = this;
        wx.chooseImage({
            count: 9 - e.data.images.length,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(t) {
                for (var n = t.tempFilePaths, i = e.data.images, o = 0; o < n.length; o++) {
                    var s = a.util.getQiniuFileName(e.data.images.length), u = i.length;
                    e.uploadImageFromQiniu(n[o], s, u);
                    var r = {
                        fileName: s,
                        path: n[o],
                        index: u,
                        state: 0
                    };
                    i = i.concat(r);
                }
                e.setData({
                    images: i
                });
            }
        });
    },
    clickRetry: function(t) {
        var a = t.currentTarget.dataset.index, e = this.data.images, n = this.data.images[a];
        this.uploadImageFromQiniu(n.path, n.fileName, a), n.state = 0, e[a] = n, this.setData({
            images: e
        });
    },
    uploadImageFromQiniu: function(a, e, n) {
        var i = this;
        t.upload(a, function(t) {
            var a = i.data.images;
            a[n].state = 1, i.setData({
                images: a
            });
        }, function(t) {
            console.log("error: " + t);
            var a = i.data.images;
            a[n].state = -1, i.setData({
                images: a
            });
        }, {
            region: "ECN",
            uptoken: i.data.token,
            uploadURL: "https://up.qbox.me",
            key: e,
            domain: "i.hunyu.co/"
        }, function(t) {});
    },
    getQiniuToken: function() {
        var t = this;
        a.network.post({
            url: a.url.getQiniuToken,
            params: {},
            before: function() {},
            success: function(a) {
                1 == a.data.result && t.setData({
                    token: a.data.data.uploadToken
                });
            },
            fail: function() {},
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    bindSubmitTap: function() {
        var t = this.data.content, e = this.data.images;
        if (0 != e.length) {
            for (var n = "", i = 0; i < e.length; i++) {
                var o = e[i];
                if (1 != o.state) return void a.util.showToast("请等照片上传完");
                n = n + o.fileName + ",";
            }
            a.network.post({
                url: a.url.addDynamic,
                params: {
                    content: t,
                    images: n
                },
                before: function() {
                    wx.showLoading({
                        title: "正在发布"
                    });
                },
                success: function(t) {
                    if (wx.hideLoading(), 1 == t.data.result) {
                        a.util.showToast("发布成功");
                        var e = getCurrentPages();
                        e[e.length - 2].setData({
                            needClose: !0
                        }), setTimeout(function() {
                            wx.navigateBack({}), wx.showTabBar({});
                        }, 1500);
                    }
                },
                fail: function() {
                    wx.hideLoading(), a.util.showToast("发布失败，请重试");
                },
                complete: function() {}
            });
        } else a.util.showToast("请选择照片");
    },
    bindImageTap: function(t) {
        for (var a = [], e = 0; e < this.data.images.length; e++) {
            var n = this.data.images[e];
            a = a.concat(n.path);
        }
        wx.previewImage({
            current: t.currentTarget.dataset.url,
            urls: a
        });
    },
    clickDel: function(t) {
        var a = t.currentTarget.dataset.index, e = this.data.images;
        e.splice(a, 1), this.setData({
            images: e
        });
    },
    onLoad: function(t) {
        this.getQiniuToken();
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        var t = a.util.getShareInfo();
        return {
            title: a.globalData.commonShareTitle,
            path: t[0],
            imageUrl: t[1],
            success: function(t) {},
            fail: function(t) {}
        };
    }
});